home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 782 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: uu4news.netcom.com!otsrvr!Ray_Robert
  2. From: Ray_Robert@ortel.org (Ray Robert)
  3. Reply-To: Ray_Robert@ortel.org
  4. Newsgroups: comp.lang.c
  5. Distribution: world
  6. Subject: Re: A problem with fprintf
  7. Date: 09 Jan 1996 07:36:35 GMT
  8. Message-ID: <2154942365.28352469@ortel.org>
  9. Organization: Oregon Telcom
  10.  
  11. vishwajit@cis.ufl.edu writes:
  12.  
  13. > I am experiencing a peculiar problem with fprintf.
  14.  
  15. > I have a piece of code where the following statement fits in:
  16.  
  17. fprintf(fp, "%s\n", src);
  18. >fp is a file-pointer and src is a static array of characters. When the
  19. > code is executed repeatedly, it fails at the above statement. The error
  20. > message from the debugger is as follows:
  21.  
  22. > signal SEGV (segmentation violation) in malloc at 0xef778634
  23. > malloc+0x150:   st      %o0, [%l5]
  24.  
  25. > fp is pointing to a file that has been opened and is not the problem.
  26. > Does somebody have a clue?
  27.  
  28. Apparently your system's fprintf is doing an malloc() to set up a space in
  29. which to construct the formatted string (wow! what an inefficiency there). 
  30. It is probably incorrectly determining the size of "src", which it expects to
  31. be a (pointer to a) null-terminated string.  One of 2 things is happening (I
  32. leave it to the academics out there to tell us which is ANSI C):  
  33.  
  34. (1) The array is being passed by value, i.e., the literal value of the first
  35. element is being passed to fprintf and hence to malloc, but it's being
  36. interpretted as if it's a pointer.
  37.  
  38. (2) The last element of the array is not a null ('\0' in the old style, 0 in
  39. the new).
  40.  
  41. Ray Robert
  42.